// pages/home/index.js
const app = getApp()
var call = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: [], // 订单列表
    offset: 0, // 页码
    limit: 15, // 每页条数
    total: 0, // 数据总条数
    status: false, // 是否还有数据

    // 轮播图开始
    advertisement: [],
    vertical: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    // 轮播图结束

    headerHeight: 0,
    headerTop: 0,
    navOpacity: 0.0,

    showNotice: false, // 通知
    showAgreement: false, // 协议

    refresh_profile: false, // 是否需要更新信息



    // 改版
    pageStatus: false,
    noticeShow: false,
    loadShow: true,
    headerBg: '', //背景图
    recommendBg: '/image/home/recommendBg.png', //背景图

    // 消息模块
    newsListData: {}, // 消息数据
    unreadTotal: '',
    navigationListData: [{
      id: 1,
      img: '/image/home/icon01.png',
      name: '商城'
    }, {
      id: 2,
      img: '/image/home/icon02.png',
      name: '租车'
    }, {
      id: 3,
      img: '/image/home/icon03.png',
      name: '快洗'
    }, {
      id: 4,
      img: '/image/home/icon04.png',
      name: '理发'
    }, {
      id: 5,
      img: '/image/home/icon05.png',
      name: '物业'
    }, {
      id: 6,
      img: '/image/home/icon06.png',
      name: '洗车'
    }, {
      id: 7,
      img: '/image/home/icon07.png',
      name: '家政'
    }, {
      id: 8,
      img: '/image/home/icon08.png',
      name: '餐饮'
    }, {
      id: 9,
      img: '/image/home/icon09.png',
      name: '碳生活'
    }, {
      id: 10,
      img: '/image/home/icon10.png',
      name: '预约'
    }],
    upAndDownStatus: false, // 导航是否打开
    animationHeight: '', // 导航高度
    animationRotate: '', // 旋转

    // 平台推荐
    recommendListData: [], // 平台推荐数据
    userInfo: {}, // 用户信息

    // 轮播
    bannerData: [],
    currentPark: {}, // 当前园区
    couponEntrance: '', // 优惠券入口图片

    notice: {}, // 公告数据
    noticeBg: '/image/public/noticeBg.png', //背景图

    recommendPage: 1, // 平台推荐页码
    recommendLimit: 6, // 平台推荐条数
    dataStatus: false, // 数据提示
    needAuth: 1, //  是否登录 0 登录 1 没有登录

    msgList: [],
    recommendTitleData: {}
    
  },
  // 获取热搜
  getHotserlist(){
    call.httpData('GET','/api/index/hotserlist', {},
    res => {
      console.log(res,'获取平台热搜数据')
      if(res.code == 1){
        this.setData({ msgList: res.data })
      }else{
        wx.showToast({ icon: 'none',title: res.msg });
      }
      this.setData({ pageStatus: true })
    },function (err) {
      console.log(err)
    })
  },
  // 看官方公告
  seeNotice() {
    let notice = this.data.notice
    this.selectComponent('#notice').see(notice) // 触发公告组件
  },
  // 获取公告
  getSystemnotice() {
    call.httpData('GET', '/api/index/systemnotice', {},
      res => {
        console.log(res, '获取公告')
        if (res.code == 1) {
          this.setData({
            notice: res.data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg
          });
        }
      },
      function (err) {
        console.log(err)
      })
  },
  // 消息去详情
  goNewsDetails(e) {
    let {
      id,
      showname,
      couldreply
    } = e.currentTarget.dataset
    console.log(couldreply);
    if (couldreply == 0) {
      wx.navigateTo({
        url: '/pages/news/list/index?chatUUID=' + id + '&showName=' + showname
      })
    } else {
      wx.navigateTo({
        url: '/pages/news/service/index?chatUUID=' + id + '&showName=' + showname
      })
    }


    return
    let {
      orderType,
      order_id
    } = e.currentTarget.dataset.list
    switch (orderType) {
      case 1: // 预约
        // 预约详情
        wx.navigateTo({
          url: '/subPackages/pages/standard/my/order/index/details/index?id=' + order_id
        })
        break;
      case 2: // 商品
        // 商品详情
        wx.navigateTo({
          url: '/subPackages/pages/standard/my/order/shopping/details/index?id=' + order_id
        })
        break;
      case 3: // 自定义
      case 4: // 专家余额兑换商家余额
        break;
      case 5: // 团购
        // 团购详情
        wx.navigateTo({
          url: '/subPackages/pages/standard/my/order/groupPurchase/details/index?id=' + order_id
        })
        break;
    }
  },
  // 点击去领取优惠券
  getCoupons() {
    wx.navigateTo({
      url: '/pages/my/coupon/receive/index',
    })
  },

  // 获取园区信息
  getParkList() {
    call.httpData('GET', '/api/index/parklist', {},
      res => {
        console.log(res, '获取园区信息')
        if (res.code == 1) {
          let currentPark = res.data[0]
          this.setData({
            currentPark
          })
          wx.setStorageSync('currentPark', currentPark);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg
          });
        }
      },
      function (err) {
        console.log(err)
      })
  },
  // 获取背景图
  getBackground() {
    call.httpData('GET', '/api/index/background', {},
      (res) => {
        console.log(res, '获取背景图')
        if (res.code == 1) {
          this.setData({
            headerBg: res.data.index
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg
          })
        }
      }, () => {
        doFail();
      })
  },

  // 去园区
  goPark() {
    wx.navigateTo({
      url: '/pages/park/index/index',
    })
  },
  // 去搜索
  goSearch() {
    wx.navigateTo({
      url: '/subPackages/pages/standard/home/list/allSearch/index/index',
    })

    // wx.navigateTo({
    //   url: '/subPackages/pages/standard/home/search/index/index',
    // })
  },
  // 看详情
  goDetails(e) {
    let {
      diff,
      obj
    } = e.currentTarget.dataset
    let {
      id,
      type
    } = obj
    if (diff) {
      let {
        location_id,
        location_type
      } = obj // type 1商品 2预约 3文章
      id = location_id
      type = location_type
    }

    switch (type) {
      case 1:
        wx.navigateTo({
          url: '/subPackages/pages/standard/home/recommend/shoppingDetails/index?id=' + id
        })
        break;
      case 2:
        wx.navigateTo({
          url: '/subPackages/pages/standard/home/recommend/serviceDetails/index?id=' + id
        })
        break;
      case 3:
        wx.navigateTo({
          url: '/subPackages/pages/standard/home/list/articleDetails/index?id=' + id
        })
        break;
      case 4:
        wx.navigateTo({
          url: '/subPackages/pages/standard/activity/index/index?id=' + id
        })
        break;
    }
  },
  // 去账单
  goBill() {
    wx.navigateTo({
      url: '/subPackages/pages/standard/my/bill/index/index',
    })
  },
  // 去更多消息列表
  goNews() {
    // wx.navigateTo({
    //   url: '/subPackages/pages/standard/my/news/index/index',
    // })
    wx.navigateTo({
      url: '/pages/news/index/index',
    })


  },
  // 下拉出所有导航
  upAndDownClick() {
    let upAndDownStatus = !this.data.upAndDownStatus
    this.setData({
      upAndDownStatus: upAndDownStatus
    })
    console.log(upAndDownStatus)
    // 高度
    let animationHeight = wx.createAnimation({
      duration: 500,
      timingFunction: "linear",
    })
    // 旋转
    let animationRotate = wx.createAnimation({
      duration: 500,
      timingFunction: "linear",
    })
    // 3行 - 430、2行 - 290
    upAndDownStatus ? animationHeight.height('290rpx').step() : animationHeight.height('154rpx').step()
    upAndDownStatus ? animationRotate.rotate(180).step() : animationRotate.rotate(0).step()

    this.setData({
      animationHeight: animationHeight.export(),
      animationRotate: animationRotate.export()
    })
  },
  // 点击导航
  navigationList(e) {
    let id = e.currentTarget.dataset.id
    let obj = {}
    switch (id) {
      case 1: // 199商城
        //this.goShoppingMall()
        wx.redirectTo({
          url: '/subPackages/pages/standard/home/index/index'
        })
        break;
      case 2: // 199租车
        this.goCarRental()
        break;
      case 3: // 199快洗
        obj = {
          id: 29,
          reekoIndex: 7,
          secondLevel: 1,
          secondLevelId: 45,
          title: "生活服务",
          type: 1,
        }
        wx.navigateTo({
          url: '/subPackages/pages/standard/home/list/index/index?obj=' + JSON.stringify(obj)
        })
        break;
      case 4: // 199理发
        obj = {
          id: 16,
          reekoIndex: 1,
          secondLevel: 3,
          secondLevelId: 40,
          title: "生活休闲",
          type: 1
        }
        wx.navigateTo({
          url: '/subPackages/pages/standard/home/list/index/index?obj=' + JSON.stringify(obj)
        })
        break;
      case 5: // 199物业
        this.goWork()
        break;
      case 6: // 199洗车
        obj = {
          id: 9,
          reekoIndex: 2,
          secondLevel: 2,
          secondLevelId: 42,
          title: "商旅出行",
          type: 1,
        }
        wx.navigateTo({
          url: '/subPackages/pages/standard/home/list/index/index?obj=' + JSON.stringify(obj)
        })
        break;
      case 7: // 199家政
        obj = {
          id: 29,
          reekoIndex: 7,
          secondLevel: 1,
          secondLevelId: 44,
          title: "生活服务",
          type: 1,
          reeko: true,
        }
        wx.navigateTo({
          url: '/subPackages/pages/standard/home/list/index/index?obj=' + JSON.stringify(obj)
        })
        break;
      case 8: // 199餐饮
        obj = {
          id: 9,
          reekoIndex: 2,
          secondLevel: 0,
          secondLevelId: 27,
          title: "商旅出行",
          type: 1,
        }
        wx.navigateTo({
          url: '/subPackages/pages/standard/home/list/index/index?obj=' + JSON.stringify(obj)
        })
        break;
      case 9: // 碳生活
        this.goCarbonAccount()
        break;
      case 10: // 199预约
        this.goAppointment()
        break;
      case 11: // 生鲜促销
        this.goSpecialArea(1)
        break;
      case 12: // 节日祝福
        this.goSpecialArea(2)
        break;
      case 13: // 生活休闲
        this.goSpecialArea(3)
        break;
    }
  },
  // 去活动专区列表
  goSpecialArea(id) {
    wx.navigateTo({
      url: '/subPackages/pages/standard/activity/index/index?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let recommendBg = wx.getFileSystemManager().readFileSync(this.data.recommendBg, 'base64'); //背景图转义
    let noticeBg = wx.getFileSystemManager().readFileSync(this.data.noticeBg, 'base64'); //背景图转义
    this.setData({
      recommendBg: 'data:image/jpg;base64,' + recommendBg,
      noticeBg: 'data:image/jpg;base64,' + noticeBg,

    })

    //this.user(options);
    //this.listData(this.data.offset,this.data.limit);

    // this.isAgreement()// 是否同意协议
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let statusBarHeight = res.statusBarHeight // 状态栏的高度
        let navTop = menuButtonObject.top * 2
        let navObjWid = res.windowWidth - menuButtonObject.right + menuButtonObject.width // 胶囊按钮与右侧的距离 = windowWidth - right+胶囊宽度
        let navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        let capsuleHeight = menuButtonObject.height * 2; //胶囊高度
        console.log(`导航栏总体高度${navHeight},胶囊距离顶部距离${navTop}.height,胶囊宽度(包括右边距离)${navObjWid}`)
        console.log(menuButtonObject)
        this.setData({
          navHeight,
          navTop,
          capsuleHeight
        })
      },
      fail(err) {
        console.log(err);
      }
    })


    this.getSystemnotice() // 获取公告
    this.getBanner() // 获取banner
    this.getGoodslist() // 获取平台推荐数据
    this.getParkList() // 获取园区信息
    this.getBackground() // 获取背景图
    this.getCoupon() // 获取优惠券入口图片
    //this.mobilelogin() // 和199精品服务互通token

    this.getHotserlist() // 获取平台热搜数据
  },
  // 执行
  onShow() {
    wx.cloud.init()
    let that = this
    wx.cloud.callContainer({
      config: {
        env: 'prod-0g38oh8hf0e4d5ac',
      },
      path: '/v1/user/login',
      method: 'GET',
      header: {
        'X-WX-SERVICE': 'springboot-069k',
      }
    }).then(res => {
      let Data = res.data.data
      console.log(Data, '登录成功')

      that.setData({
        needAuth: Data.needAuth
      });
      if (Data.needAuth == 0) { //  有数据 登录成功
        wx.setStorageSync('userInfo', Data.userInfo);
        wx.setStorageSync('token', Data.userInfo.mobile);
        wx.setStorageSync('openId', Data.openId);
        that.setData({
          userInfo: Data.userInfo
        });
      } else {
        wx.setStorageSync('userInfo', {});
        that.setData({
          userInfo: {}
        });
      }

      that.setData({
        pageStatus: true,
      })
      // that.setData({
      //   loadShow:false,
      //   noticeShow:true
      // })
      // setTimeout(() => {
      //   that.setData({ pageStatus: true , noticeShow:false})
      // }, 2000);
      this.getMaillist() // 获取消息

    })
    // 定时器 判断是否有新消息
    this.getUnread()
    this.setTimer()
  },
  getUnread() {
    wx.cloud.callContainer({
      "config": {
        "env": "prod-0g38oh8hf0e4d5ac"
      },
      "path": "/v1/chat/unReadNum",
      "header": {
        "X-WX-SERVICE": "springboot-069k"
      },
      "method": "get",
      "data": {
        "action": "inc"
      }
    }).then(res => {
      console.log(res, '定时器');
      if (res.data.code == 1) {
        // if (res.data.data > 0) {
        this.setData({
          'unreadTotal': res.data.data
        })
        if (res.data.data > 0) {
          this.getMaillist()
        }
      } else {
        if (this.data.timer) {
          console.log('卸载定时器');
          clearInterval(this.data.timer);
          this.setData({
            timer: null
          })
        }
      }
    })
  },

  setTimer() {
    let _this = this
    let timer = setInterval(_this.getUnread, 5000)
    this.setData({
      timer
    })
  },
  // 获取用户信息
  user(options) {
    call.request('/api/user/profile', {},
      (data) => {
        if (data.code == 1) {
          let userInfo = data.data;
          if ('wechat_openid' in options && options.wechat_openid) {
            userInfo.wechat_openid = options.wechat_openid;
            wx.setStorageSync('userInfo', userInfo);
          }
          wx.setStorageSync('userInfo', userInfo);
          this.setData({
            refresh_profile: userInfo.refresh_profile,
            userInfo
          })

          this.mobilelogin() // 和199精品服务互通token
          console.log('获取用户信息', userInfo)
        } else {
          wx.showToast({
            icon: 'none',
            title: data.msg
          })
        }
      },
      function (err) {

      })
  },
  // 和199精品服务互通token
  mobilelogin() {
    call.httpData('POST', '/api/user/mobilelogin', {},
      res => {
        console.log(res, '和199精品服务互通token')
        if (res.code == 1) {



        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg
          });
        }
      },
      function (err) {
        console.log(err)
      })
  },
  // 获取优惠券入口图片
  getCoupon() {
    call.httpData('GET', '/api/index/coupon', {},
      (res) => {
        console.log(res, '获取优惠券入口图片')
        if (res.code == 1) {
          let couponEntrance = res.data
          this.setData({
            couponEntrance
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg
          })
        }
      }, () => {
        doFail();
      })
  },
  // 获取消息
  async getMaillist() {
    const {
      data: {
        data,
        code,
        errorMsg
      }
    } = await wx.cloud.callContainer({
      "config": {
        "env": "prod-0g38oh8hf0e4d5ac"
      },
      // "path": "/v1/chat/18511581997",
      "path": "/v1/chat/simple",
      "header": {
        "X-WX-SERVICE": "springboot-069k"
      },
      "method": "GET",
    })
    console.log(data, code, errorMsg)
    if (code == 1) {
      let newsListData = data
      this.setData({
        newsListData
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: errorMsg
      })
    }
    // call.httpData('GET','/api/user/maillist',{ page: 1,limit: 2},
    // (res) => {
    //   console.log(res,'获取消息数据')
    //   if(res.code == 1){
    //     let newsListData = res.data
    //     this.setData({ newsListData })
    //   }else{
    //     wx.showToast({ icon: 'none',title: res.msg })
    //   }
    // },() => {
    //   doFail();
    // })
  },
  // 获取banner
  getBanner() {
    call.httpData('GET', '/api/index/banner', {},
      res => {
        console.log(res, '拿取banner')
        if (res.code == 1) {
          this.setData({
            bannerData: res.data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg
          });
        }
      },
      function (err) {
        console.log(err)
      })
  },
  // 获取平台推荐数据
  getGoodslist(status) {
    let page = this.data.recommendPage // 平台推荐数据页码
    let limit = this.data.recommendLimit // 平台推荐数据条数
    call.httpData('GET', '/api/index/goodslist', {
        type: 1,
        page,
        limit
      },
      res => {
        console.log(res, '获取平台推荐数据')
        this.setData({
          recommendTitleData: {
            title:res.data.title,
            icon:res.data.icon
          }
        })
        if (res.code == 1) {
          let list = res.data.goodsList
          if (status) { // 触发搜索
            let recommendListData = this.data.recommendListData // 数据
            recommendListData = recommendListData.concat(list)
            this.setData({
              recommendListData
            })
          } else {
            this.setData({
              recommendListData: list
            })
          }
          if (list.length < limit && page != 1) {
            this.setData({
              dataStatus: true
            })
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg
          });
        }

        wx.hideToast() // 隐藏
      },
      function (err) {
        console.log(err)
      })
  },







  // 推荐详情
  details(e) {
    console.log(e.currentTarget.dataset.id)
    // 活动详情
    wx.navigateTo({
      url: '/pages/life/details/index?id=' + e.currentTarget.dataset.id
    })
  },
  // 卡包
  card() {
    wx.navigateTo({
      url: '/pages/card/index'
    })
  },
  // 付款码
  paymentCode() {
    wx.navigateTo({
      url: '/pages/paymentCode/balance/index'
    })
  },
  // 通行码
  goThroughCode() {
    wx.navigateTo({
      url: '/pages/paymentCode/goThrough/index'
    })
  },
  // 扫一扫
  scanCode() {
    wx.scanCode({
      success(res) {
        call.getData('/api/common/scene', {
            mark: res.result
          },
          function (data) {
            console.log(data)
            if (data.code == 1) {
              if (data.data.sence == 1) {
                wx.navigateTo({
                  url: '/pages/order/confirm/index?' + res.result
                })
              } else if (data.data.sence == 2) {
                wx.showLoading({
                  title: '取件中',
                  mask: true
                })
                call.request('/api/cabinet/scan', {
                    unicode: res.result
                  },
                  function (datas) {
                    console.log(datas.data.num)
                    let status = 1
                    if (datas.status == 1) {
                      status = 3
                    } else if (datas.data.num == 0) {
                      status = 2
                    }
                    wx.hideLoading();
                    wx.navigateTo({
                      url: '/pages/pickUpResults/index?status=' + status
                    })
                    console.log(status)
                  },
                  function (err) {

                  })
              } else if (data.data.sence == 3) {
                let orders = JSON.stringify([data.data.order.id]);
                wx.navigateTo({
                  url: '/pages/invoice/fillIn/index?orders=' + orders + "&amount=" + data.data.order.order_values
                })
              }
              console.log(data.data.sence)
            } else {
              wx.showToast({
                icon: 'none',
                title: data.msg
              })
            }
          },
          function (err) {

          })
        console.log(res)
        // if(res.result.startsWith('orderNumber')){
        //   wx.navigateTo({
        //     url: '/pages/order/confirm/index?' + res.result
        //   })
        // }
      }
    })
  },
  // 余额
  balance() {
    wx.navigateTo({
      url: '/pages/balance/index/index'
    })
  },
  // 去往商城
  goShoppingMall() {
    wx.navigateToMiniProgram({
      appId: 'wx1a8424bde1243d49',
      path: '',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release', // develop	开发版 trial	体验版 release	正式版
      success(res) {
        // 打开成功
        console.log(1)
      }
    })
  },
  // 去往租车
  goCarRental() {
    wx.navigateToMiniProgram({
      appId: 'wxbe897e6b1143e442',
      path: '',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release', // develop	开发版 trial	体验版 release	正式版
      success(res) {
        // 打开成功
        console.log(1)
      }
    })
  },
  // 去往碳账户
  goCarbonAccount() {
    wx.navigateToMiniProgram({
      appId: 'wxdf785a17c1f1b6d2',
      path: '',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release', // develop	开发版 trial	体验版 release	正式版
      success(res) {
        // 打开成功
        console.log(1)
      }
    })
  },
  // 去往超市和点餐
  jump(e) {
    wx.navigateTo({
      url: '/pages/Jump/index?name=' + e.currentTarget.dataset.name
    })
  },
  // 产品预定
  goAppointment() {
    wx.navigateTo({
      url: '/pages/bookingGoods/index/index'
    })
  },
  // 未开放功能
  Public() {
    wx.showToast({
      icon: 'none',
      title: '加紧施工中...',
    })
  },
  // 工作
  goWork() {
    wx.redirectTo({
      url: '/pages/work/index/index'
    })
  },
  // 生活
  goLife() {
    wx.redirectTo({
      url: '/subPackages/pages/standard/home/index/index'
    })
  },
  // 消息
  goNew() {
    let userInfo = wx.getStorageSync('userInfo') // 用户信息
    if (JSON.stringify(userInfo) == "{}") {
      let isSignIn = this.selectComponent('#isSignIn'); // 页面获取自定义组件实例
      isSignIn.isLogin(); // 通过实例调用组件事件
    } else {
      wx.redirectTo({
        url: '/pages/news/index/index'
      })
    }

  },
  // 我的
  goMy() {
    let userInfo = wx.getStorageSync('userInfo') // 用户信息
    if (JSON.stringify(userInfo) == "{}") {
      let isSignIn = this.selectComponent('#isSignIn'); // 页面获取自定义组件实例
      isSignIn.isLogin(); // 通过实例调用组件事件
    } else {
      wx.redirectTo({
        url: '/pages/my/index/index'
      })
    }

  },
  // 看看是否登录
  isLoginPublic() {
    let userInfo = wx.getStorageSync('userInfo') // 用户信息
    if (JSON.stringify(userInfo) == "{}") {
      let isSignIn = this.selectComponent('#isSignIn'); // 页面获取自定义组件实例
      isSignIn.isLogin(); // 通过实例调用组件事件
    }

  },
  // 获取数据
  listData(offset, limit) {

  },
  /**
   * 页面上拉触底事件的处理函数，与点击加载更多做同样的操作
   */
  onReachBottom() {
    let page = this.data.recommendPage; // 页码
    let dataStatus = this.data.dataStatus
    if (!dataStatus) {
      this.setData({
        recommendPage: page + 1
      })
      this.getGoodslist(true) // 获取列表数据

      wx.showToast({
        title: '加载中...',
        icon: 'loading',
      })

    }
  },

  // 通知
  onNoticeClose() {
    this.setData({
      showNotice: false
    })
    wx.setStorageSync('notice', true)
  },

  // 更新用户信息
  getUserProfile(e) {
    let that = this
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于显示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res.userInfo)
        let {
          avatarUrl,
          nickName,
          gender
        } = res.userInfo
        let sex = 1 // 0=未知,1=男,2=女
        gender == 0 ? sex = 1 : sex = 2
        call.request('/api/user/refreshProfile', {
            avatar: avatarUrl,
            nickname: nickName,
            sex
          },
          res => {
            console.log(res, '把头像和昵称传入后台')
          },
          function (err) {
            console.log(err)
          })
      },
      complete(end) {
        that.setData({
          refresh_profile: false
        })
      }
    })
  },
  // 是否同意协议
  isAgreement() {
    call.request('/api/user/checkagreement', {},
      (data) => {
        if (data.code == 0) {
          this.setData({
            showAgreement: true
          })
        }
      },
      function (err) {})
  },
  // 不同意
  no() {
    wx.reLaunch({
      url: '/pages/login/index'
    })
    wx.clearStorage()
  },
  // 同意
  yes() {
    call.request('/api/user/tickagreement', {
        tick: 1
      },
      (data) => {
        if (data.code == 1) {
          this.setData({
            showAgreement: false
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: data.msg
          })
        }
      },
      function (err) {

      })
  },
  // 去用户协议
  one() {
    wx.navigateTo({
      url: '/pages/agreement/one/index'
    })
  },
  // 去隐私政策
  two() {
    wx.navigateTo({
      url: '/pages/agreement/two/index'
    })
  },
  onReady() {
    let mb = wx.getMenuButtonBoundingClientRect();
    console.log(mb)
    this.setData({
      headerHeight: mb.height * 2,
      headerTop: mb.top * 2
    })
  },
  /**
   * 页面滚动
   * @param {*} ev 
   */
  onPageScroll: function (e) {
    if (e.scrollTop < 30 && this.data.navOpacity != 0.0) {
      this.setData({
        navOpacity: 0.0,
      });
    } else if (e.scrollTop > 40 && this.data.navOpacity != 1) {
      this.setData({
        navOpacity: 1,
      });
    }
  },
  onHide: function () {
    console.log('隐藏');
    if (this.data.timer) {
      console.log('卸载定时器');
      clearInterval(this.data.timer);
      this.setData({
        timer: null
      })
    }
  },
  onUnload: function () {
    console.log('卸载');
    if (this.data.timer) {
      console.log('卸载定时器');
      clearInterval(this.data.timer);
      this.setData({
        timer: null
      })
    }
  }
})